package com.cy.pj.common.web;

import com.cy.pj.common.exception.ServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//@ControllerAdvice//此注解描述的类为全局异常处理类
//@ResponseBody
@RestControllerAdvice //==@ControllerAdvice + @ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    @ResponseBody
    public String doHandleArrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e){

        return "数组下标越界";
    }


    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public String doHandleArithmeticException(ArithmeticException e){

        return "the exception msg is "+e.getMessage();
    }

    @ExceptionHandler({ServiceException.class})
    @ResponseBody
    public String ServiceException(ServiceException e){

        return "the exception msg is "+e.getMessage();
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public String doHandleRuntimeException(RuntimeException e){
        e.printStackTrace();
        return "Runtime Exception"+e.getMessage();
    }

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public String doHandleThrowable(Throwable e){//此方法为异常处理方法的兜底方法
        e.printStackTrace();
        return "系统维护中";
    }

}
